function [mesh,row_idx] = smooth_C0_dis(dofs,mesh,cr)
% function [H,row_idx] = smooth_C0_dis(dofs,V,T,E,posE,ET,TE,d,cr)
% get the global smoothness conditions idx_mat = zeros(3,d,3); % the column idx 1 2 3 -1 -2 -3
V=mesh.V;
T=mesh.T;
ET=mesh.ET;
TE=mesh.TE;
E=mesh.E;
d=mesh.d;
posE=size(E,1);

idx_c0 = zeros(d+1,3);
for i = 1:3
    idx_c0(:,i) = cr_indices(0,d,i,cr);
end
E_in = find(ET(:,2)~=0); nE_in = length(E_in);
row_idx = zeros(nE_in*(d+1),1); % idx for nei
col_idx = zeros(nE_in*(d+1),1); % idx for tri
pos = 1; 
for k = 1:nE_in
    eg = E_in(k);
    tri = ET(eg,1); nei = ET(eg,2); % find two triangles who have eg.
    eg2tri = find(TE(tri,:)==eg);  % local index of eg in tri
    eg2nei = find(TE(nei,:)==eg); % local index of nei in tri
    % first set c0 condition:
    col_idx(pos:pos+d) = dofs(tri,idx_c0(:,eg2tri))';
    % set the row index for nei:
    line = dofs(nei,idx_c0(:,eg2nei))';
    if mod(eg2tri+eg2nei,2)==1 % the same direction with v2->v3
        row_idx(pos:pos+d) = line;
            else  % the different direction in neighbour
       row_idx(pos+d:-1:pos) = line;
         
    end
    pos = pos + d + 1;
end
dim = max(max(dofs))-min(min(dofs))+1; pos = pos-1;
mesh.H = sparse((1:pos)',col_idx,ones(pos,1),pos,dim)-sparse((1:pos)',row_idx,ones(pos,1),pos,dim); 